/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.unicode.unihan.domain;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author Andrew
 */
public class UnihanInfo {

    private Integer unicode;
    private Map<String, String> properties = new HashMap<String, String>();

    public UnihanInfo() {
    }

    public UnihanInfo(Integer unicode) {
        this.unicode = unicode;
    }

    // TODO: should this be a public constructor?  Is it used outside of testing?
    // TODO: should this be a factory?
    public UnihanInfo(UnihanRow... unihanRows) {
        for (UnihanRow unihanRow : unihanRows) {
            if (unicode == null) {
                unicode = unihanRow.getUnicode();
            } else {
                if (! unicode.equals(unihanRow.getUnicode())) {
                    throw new IllegalArgumentException("The unicode values must all be the same");
                }
            }
            properties.put(unihanRow.getKey(), unihanRow.getValue());
        }
    }

    /**
     * @return the unicode
     */
    public Integer getUnicode() {
        return unicode;
    }

    public void addProperty(String key, String value) {
        properties.put(key, value);
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    public Iterable<Map.Entry<String, String>> getProperties() {
        return properties.entrySet();
    }

    @Override
    public String toString() {
        return "Unicode: " + Integer.toHexString(getUnicode()) + ", properties: " +
                properties.toString();
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + (this.getUnicode() != null ? this.getUnicode().hashCode() : 0) +
                properties.hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UnihanInfo other = (UnihanInfo) obj;
        if (this.getUnicode() != other.getUnicode() &&
                (this.getUnicode() == null || !this.unicode.equals(other.unicode))) {
            return false;
        }

        return other.properties.equals(properties);

    }
}
